$('document').ready(function () {
    // 加载页面时触发的事件
    if(parseInt(getCookie('code'))!==202){
        window.location.href="home.html"
    }
 
    // 获取学校列表
    var totalPageNum = 0
    $.ajax({
        type: 'GET',
        url: 'http://111.229.120.67/adm/get-sch-list',
        data: {
            "pageNum": 1,
            "pageSize": 30
        },
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            if (data.statusCode == 0) {
                console.log(data)
                totalPageNum = data.page.totalPageNum
                if (totalPageNum > 1) {
                    for (var i = 2; i < totalPageNum + 1; i++) {
                        $('#raquo').before(`
                        <li id="page` + i + `"><a href="javascript:void(0)">` + i + `</a></li>
                        `)
                    }
                } else {
                    $('#pagination').css('display', 'none')
                }
                var array = data.page.schoolList
                for (var i = 0; i < array.length; i++) {
                    $('#list-tbody').append(`
                        <tr>
                            <td>` + array[i].schid + `</td>
                            <td>` + array[i].schname + `</td>
                            <td>` + array[i].schtype + `</td>
                            <td>` + array[i].schaddress + `</td>
                            <td>` + array[i].schphone + `</td>
                            <td>
                                <a href="javascript:void(0)" class="school-upade">修改</a>
                                <a href="javascript:void(0)" class="school-remove">删除</a>                
                            </td>
                        </tr>
                    `)
                }
            } else {
                $('#myModal-public').modal({})
                $('#myModal-public h4').html('请求超时')
                $('#myModal-public-body').html('请求超时，请重试')
            }
        }
    })
    // 分页
    $('#pagination').on('mouseover', function () {
        $('#pagination li').off('click').click(function () {
            $('#pagination li').removeClass("active")
            $('#pagination li').eq($(this).text()).addClass("active")
            // console.log($(this).text())
            // console.log($('#pagination .active').text())
            if ($('#pagination .active').text() == '1') {
                $('#laquo').css('display', 'none')
            } else {
                $('#laquo').css('display', 'inline')
            }
            if ($('#pagination .active').text() == totalPageNum) {
                $('#raquo').css('display', 'none')
            } else {
                $('#raquo').css('display', 'inline')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/adm/get-sch-list',
                data: {
                    "pageNum": parseInt($(this).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    // console.log(data)
                    if (data.statusCode == 0) {
                        $('#list-tbody').html('')
                        var array = data.page.schoolList
                for (var i = 0; i < array.length; i++) {
                    $('#list-tbody').append(`
                        <tr>
                            <td>` + array[i].schid + `</td>
                            <td>` + array[i].schname + `</td>
                            <td>` + array[i].schtype + `</td>
                            <td>` + array[i].schaddress + `</td>
                            <td>` + array[i].schphone + `</td>
                            <td>
                                <a href="javascript:void(0)" class="school-upade">修改</a>
                                <a href="javascript:void(0)" class="school-remove">删除</a>                
                            </td>
                        </tr>
                    `)
                }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#laquo').off('click').click(function () {
            var active = $('#pagination .active').text()
            var laquo = $('#pagination li').eq(active - 1).text()
            $('#pagination .active').removeClass("active")
            $('#pagination li').eq(laquo).addClass("active")
            if (laquo == 1) {
                $('#laquo').css('display', 'none')
            }
            if ($('#pagination .active').text() !== totalPageNum) {
                $('#raquo').css('display', 'inline')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/adm/get-sch-list',
                data: {
                    "pageNum": parseInt($('#pagination li').eq(laquo).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#list-tbody').html('')
                        var array = data.page.schoolList
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                                <tr>
                                    <td>` + array[i].schid + `</td>
                                    <td>` + array[i].schname + `</td>
                                    <td>` + array[i].schtype + `</td>
                                    <td>` + array[i].schaddress + `</td>
                                    <td>` + array[i].schphone + `</td>
                                    <td>
                                        <a href="javascript:void(0)" class="school-upade">修改</a>
                                        <a href="javascript:void(0)" class="school-remove">删除</a>                
                                    </td>
                                </tr>
                            `)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
        $('#raquo').off('click').click(function () {
            var active = $('#pagination .active').text()
            var raquo = $('#pagination li').eq(parseInt(active) + 1).text()
            $('#pagination .active').removeClass("active")
            $('#pagination li').eq(raquo).addClass("active")
            $('#laquo').css('display', 'inline')
            if ($('#pagination .active').text() == totalPageNum) {
                $('#raquo').css('display', 'none')
            }
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/adm/get-sch-list',
                data: {
                    "pageNum": parseInt($('#pagination li').eq(raquo).text()),
                    "pageSize": 30
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    console.log(data)
                    if (data.statusCode == 0) {
                        $('#list-tbody').html('')
                        var array = data.page.schoolList
                        for (var i = 0; i < array.length; i++) {
                            $('#list-tbody').append(`
                                <tr>
                                    <td>` + array[i].schid + `</td>
                                    <td>` + array[i].schname + `</td>
                                    <td>` + array[i].schtype + `</td>
                                    <td>` + array[i].schaddress + `</td>
                                    <td>` + array[i].schphone + `</td>
                                    <td>
                                        <a href="javascript:void(0)" class="school-upade">修改</a>
                                        <a href="javascript:void(0)" class="school-remove">删除</a>                
                                    </td>
                                </tr>
                            `)
                        }
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
        })
    })
    // -------------------------------------------
    var school_id=0  //学校ID全局变量
    $('#list-tbody').on('mouseover',function(){
        $('.school-upade').click(function () {
            var upade_index = $('.school-upade').index($(this)[0])
            var schid = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(0).html()
            var schname = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(1).html()
            var schtype = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(2).html()
            var schaddress = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(3).html()
            var schphone = $('#table-responsive tbody tr:eq(' + upade_index + ') td').eq(4).html()
            $('#myModal-public h4').html('修改学校信息(ID:' + schid + ')')
            $('#myModal-public-body').html(`
                名称：<input type="text" id="schname" value="`+ schname+`" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                类别：<input type="text" id="schtype" value="`+schtype+`" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                地址：<input type="text" id="schaddress" value="`+schaddress+`" placeholder="不填写表示该项保留原状">
                <br>
                <br>
                电话：<input type="text" id="schphone" value="`+schphone+`" placeholder="不填写表示该项保留原状">
            `)
            $('#myModal-public .modal-footer').html(
                `<button type="button" id="surebtn" class="btn btn-primary">确认修改
        </button><button type="button" class="btn" data-dismiss="modal">关闭</button>`
            )
            $('#myModal-public').modal({
    
            })
        })
        $('.school-remove').click(function () {
            var remove_index = $('.school-remove').index($(this)[0])
            var schid = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(0).html()
            var schname = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(1).html()
            var schtype = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(2).html()
            var schaddress = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(3).html()
            var schphone = $('#table-responsive tbody tr:eq(' + remove_index + ') td').eq(4).html()
            $('#myModal-public h4').html('删除ID('+schid+')学校')
            $('#myModal-public-body').html(`
                名称：`+ schname+`
                <br>
                <br>
                类别：`+schtype+`
                <br>
                <br>
                地址：`+schaddress+`
                <br>
                <br>
                电话：`+schphone+`
            `)
            $('#myModal-public .modal-footer').html(`
            <button type="button" id="sureremove" class="btn btn-primary">确认删除</button>
            </button><button type="button" class="btn" data-dismiss="modal">关闭</button>
            `)
            $('#myModal-public').modal({
    
            })
        })
    })
    
    // 确认修改 or 确认修改
    $('#myModal-public').on('mouseenter', function () {
        $('#surebtn').off('click').click(function () {
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/adm/update-sch',
                data: {
                    "schid": school_id,
                    "schname":$('#schname').val(),
                    "schtype":$('#schtype').val(),
                    "schaddress":$('#schaddress').val(),
                    "schphone":$('#schphone').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public-body').html('修改成功')
                        $('#myModal-public .modal-footer').html('<button type="button" class="btn" data-dismiss="modal">关闭</button>')
                        setTimeout(function(){
                            window.location.href="school-list.html"
                        },1000)
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
            
        })
        $('#sureremove').click(function () {
            $.ajax({
                type: 'GET',
                url: 'http://111.229.120.67/adm/del-sch',
                data: {
                    "schid": school_id,
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public-body').html('删除成功')
                        $('#myModal-public .modal-footer').html('<button type="button" class="btn" data-dismiss="modal">关闭</button>')
                        setTimeout(function(){
                            window.location.href="school-list.html"
                        },1000)
                    } else {
                        $('#myModal-public h4').html('请求超时')
                        $('#myModal-public-body').html('请求超时，请重试')
                    }
                }
            })
           
        })
    })

    
    // --------------------------
})